local Stack = Class(function(self, name)
    self.stack_table = {}
    self.size = 0
    self.name = name or ''
end)

function Stack:push(element)
    self.size = self.size + 1
    self.stack_table[self.size] = element
end

function Stack:pop()
    if self:isEmpty() then
        print("Error: Stack is empty!")
        return
    end
    local size = self.size
    self.size = size - 1
    return table.remove(self.stack_table, size)
end

function Stack:top()
    if self:isEmpty() then
        print("Error: Stack is empty!")
        return
    end
    return self.stack_table[self.size]
end

function Stack:isEmpty()
    return self.size == 0
end

function Stack:clear()
    self.stack_table = nil
    self.stack_table = {}
    self.size = 0
end

function Stack:__tostring()    
    local size = self.size
    local str = "Stack["..size.."]{"
    while size > 0 do
        str = str..", "..tostring(self.stack_table[size])
        size = size - 1
    end
    str = str.."}"
    return str
end

return Stack